package com.sms.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sms.client.UserLoginService;
import com.sms.shared.SessionInfo;

public class UserLoginServiceImpl extends RemoteServiceServlet implements UserLoginService{

	@Override
	public List checkLogin(String userName, String password) {

		List resultMap = new ArrayList();
		HibernateServiceImpl hibernateServiceImpl = new HibernateServiceImpl();

		List userList = hibernateServiceImpl.findObjects("User", new String[] { "party.partyId","party.name","roleName" }, "userName='" + userName + "' AND password='"+password+"'");
		
		int userSize = userList == null ? 0 : userList.size();
		
		if(userSize > 0){
			ArrayList dataValueList = (ArrayList) userList.get(0);
			resultMap.add(populateUserInSession(dataValueList));
			return resultMap;
		}

		resultMap.add("Authentication Failed");
		return resultMap;
	}
	

	private Object populateUserInSession(ArrayList dataValueList) {
		Long partyId = (Long)dataValueList.get(0);
		String partyName = (String)dataValueList.get(1);
		String roleName = (String)dataValueList.get(2);
		
		HttpSession httpSession = getThreadLocalRequest().getSession(true);
		String JSessionID = httpSession.getId();
		SessionInfo userSessionInfo = SessionInfo.getSharedInstance();
		userSessionInfo.clearSessionMap();

		HibernateServiceImpl hibernateServiceImpl = new HibernateServiceImpl();
		userSessionInfo.putSessionData(SessionInfo.SESSION_PARTYID, String.valueOf(partyId));
		userSessionInfo.putSessionData(SessionInfo.SESSION_PARTYNAME, partyName);
		userSessionInfo.putSessionData(SessionInfo.SESSION_ROLENAME, roleName);
		if(roleName.equalsIgnoreCase("Member")){
		List meterList = hibernateServiceImpl.findObjects("PartySiteRelation", new String[] { "site.id","site.equipments.displayGroupNo","site.name","site.invertorType.name" }, "PartySiteRelation.party.partyId=" + partyId );
			if(meterList.size() == 1){
				ArrayList siteValueList = (ArrayList)meterList.get(0);
				userSessionInfo.putSessionData(SessionInfo.SESSION_SITEID, String.valueOf((Long)siteValueList.get(0)));
				userSessionInfo.putSessionData(SessionInfo.SESSION_SITEGROUPNO, (String)siteValueList.get(1));
				userSessionInfo.putSessionData(SessionInfo.SESSION_SITENAME, (String)siteValueList.get(2));
				userSessionInfo.putSessionData(SessionInfo.SESSION_INVERTERTYPE, (String)siteValueList.get(3));
			}
		}

		userSessionInfo.putSessionData(SessionInfo.JSESSION_ID, JSessionID);
		httpSession.setMaxInactiveInterval((int) 1800);
		System.err.println("userSessionInfo : " + userSessionInfo.toString());
		httpSession.setAttribute(httpSession.getId(), userSessionInfo);
		return userSessionInfo;
	}
	
	public Map setSessionData(String siteGroupNo, String inverterType,String sessionID) {
		String partyId = null;
		String roleName = null;
		//String siteId = null;
		//String siteGroupNo = null;
		String partyName = null;
		String jSessionId =  null;
		try {
			Map<String, Object> resultMap = getSessionData(sessionID, true);

			if (resultMap == null) {
				resultMap = new HashMap();
				resultMap.put("result", "Session Invalid");
				return resultMap;
			}
			MeterServiceImpl meterServiceImpl = new MeterServiceImpl();
			List dataList = meterServiceImpl.getSiteBydisplayGroupNo(siteGroupNo);
			String siteId = String.valueOf((Long)dataList.get(0));
			String siteName = (String)dataList.get(1);
			SessionInfo userSessionInfo = (SessionInfo) resultMap.get("result");
			if (userSessionInfo != null) {

				if (userSessionInfo.getSessionData(SessionInfo.SESSION_PARTYID) != null)
					partyId = (String) userSessionInfo.getSessionData(SessionInfo.SESSION_PARTYID);

				if (userSessionInfo.getSessionData(SessionInfo.SESSION_PARTYNAME) != null)
					partyName = (String) userSessionInfo.getSessionData(SessionInfo.SESSION_PARTYNAME);

//				if (userSessionInfo.getSessionData(SessionInfo.SESSION_SITEID) != null) {
//					siteId = (String) userSessionInfo.getSessionData(SessionInfo.SESSION_SITEID);
//				}
//				if (userSessionInfo.getSessionData(SessionInfo.SESSION_SITEGROUPNO) != null) {
//					siteGroupNo = (String) userSessionInfo.getSessionData(SessionInfo.SESSION_SITEGROUPNO);
//				}
				if (userSessionInfo.getSessionData(SessionInfo.SESSION_ROLENAME) != null) {
					roleName = (String) userSessionInfo.getSessionData(SessionInfo.SESSION_ROLENAME);
				}
				if (userSessionInfo.getSessionData(SessionInfo.JSESSION_ID) != null) {
					jSessionId = (String) userSessionInfo.getSessionData(SessionInfo.JSESSION_ID);
				}
				
				
				userSessionInfo.clearSessionMap();
				userSessionInfo.putSessionData(SessionInfo.SESSION_PARTYID, partyId);
				userSessionInfo.putSessionData(SessionInfo.SESSION_PARTYNAME, partyName);
				userSessionInfo.putSessionData(SessionInfo.SESSION_ROLENAME, roleName);
				userSessionInfo.putSessionData(SessionInfo.SESSION_SITEID, siteId);
				userSessionInfo.putSessionData(SessionInfo.SESSION_SITENAME, siteName);
				userSessionInfo.putSessionData(SessionInfo.SESSION_SITEGROUPNO, siteGroupNo);
				if (inverterType != null) {
					userSessionInfo.putSessionData(SessionInfo.SESSION_INVERTERTYPE, inverterType);
				}else{
					userSessionInfo.putSessionData(SessionInfo.SESSION_INVERTERTYPE, meterServiceImpl.getInverterType(siteGroupNo));
				}
				userSessionInfo.putSessionData(SessionInfo.JSESSION_ID, jSessionId);
				// return "Success";
				return resultMap;
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	public String logout() {
		HttpSession session = getThreadLocalRequest().getSession(false);
		SessionInfo userSessionInfo = SessionInfo.getSharedInstance();
		String sessionID = null;
		try {
			// Cookie
			if (session != null) {
				String remoteIPAddress = getThreadLocalRequest().getRemoteAddr();
				System.err.println("userSessionInfo before =[" + userSessionInfo + "]");
				sessionID = session.getId();

				
				if (getThreadLocalRequest().isRequestedSessionIdValid()) {
					session.removeAttribute(sessionID);
					// session.removeAttribute(Session.SESSION_INFO);
					session.invalidate();
					userSessionInfo.clearSessionMap();
					SessionInfo.setUserSessionInfo(null);
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		System.err.println("userSessionInfo after =[" + userSessionInfo + "]");
		return sessionID;
	}

	@Override
	public Map getImages(String directoryName) {
		// TODO Auto-generated method stub
		return null;
	}
	public Map getSessionData(String sessionId, boolean throwException) {
		HttpSession httpSession = getThreadLocalRequest().getSession(false);
		Map resultMap = null;
		if (httpSession == null)
			return null;

		SessionInfo sessionInfo = (SessionInfo) httpSession.getAttribute(sessionId);
		if (sessionInfo != null) {
			resultMap = new HashMap();
			resultMap.put("result", sessionInfo);
		}
		// else {
		return resultMap;
		// }
	}

}
